package com.zimo.gui.order;

import com.zimo.download.exception.DownloadException;
import com.zimo.download.info.ServerFileINFO;
import com.zimo.gui.DownloadR;
import com.zimo.utils.StringFormat;

import java.io.IOException;
import java.net.URL;

public class Start implements Order{
    @Override
    public void handle(String[] p) {
        DownloadR downloadR = DownloadR.getDownloadR();
        if (p.length != 3) {
            System.out.println("参数不正确，请输入 help start查看");
            return;
        }
        try {
            downloadR.start(new URL(p[0]),p[1],Integer.valueOf(p[2]));
            System.out.println("开始下载："+ ServerFileINFO.getFileName(new URL(p[0]))+"  "+(ServerFileINFO.getFileSize(new URL(p[0]))/1024)+"kb");
        } catch (IOException e) {
        } catch (DownloadException e) {
        }
    }

    @Override
    public void describe() {
        System.out.println(StringFormat.formatLeftS("Start",20)+"用于开启下载 [start url filePath threadCount]");
    }

    @Override
    public void help() {
        describe();
        System.out.println("参数: ");
        System.out.println("\t"+StringFormat.formatLeftS("URL:",16)+"文件的下载地址[http://www.baidu.com/..]");
        System.out.println("\t"+StringFormat.formatLeftS("filePath:",16)+"文件的保存地址,如果文件夹不存在程序会自动创建[D://downloads]");
        System.out.println("\t"+StringFormat.formatLeftS("threadCount:",16)+"下载线程数，建议为最佳的32个[32]");
    }
}
